package controllers

import (
	"errors"
	beego "github.com/beego/beego/v2/server/web"
	"gorm.io/gorm"
	"weekFirst/global"
	"weekFirst/models"
)

type FriendController struct {
	beego.Controller
}

func (f *FriendController) Find() {
	id, _ := f.GetInt("id")

	friend := models.Friend{
		Model: gorm.Model{ID: uint(id)},
	}

	if err := global.Db.Table("friends").Where("id = ?", id).Find(&friend).Error; err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			f.CustomAbort(404, "fond")
		} else {
			f.CustomAbort(404, "fond")
		}
		return
	}

	f.Data["json"] = global.Response{
		Code:    0,
		Message: "查询OK",
		Data:    map[string]interface{}{"find": friend},
	}

	f.ServeJSON()
}
